﻿-- ===========================================================================
-- = QuestSpamOptions.lua 
-- ===========================================================================
-- DESC : This handles the options frame ui
-- ===========================================================================

-- ========= Globals =========================================================
CONTROLTYPE_CHECKBOX = 1;
CONTROLTYPE_DROPDOWN = 2;
CONTROLTYPE_SLIDER = 3;
CONTROLTYPE_EDITBOX = 4;
QuestSpamOptionsFrame = nil;
QS_UpdatedOptions = {};

-- ========= Option Panel Labels and Settings ================================

QuestSpamOptionLabels = 
{ 
	["QuestSpamOn"] = {toggle = "on", text = QUESTSPAM_ON_LABEL, tooltip = QUESTSPAM_ON_TOOLTIP },
	["QuestSpamLocal"] = {toggle = "local", text = QUESTSPAM_LOCAL_LABEL, tooltip = QUESTSPAM_LOCAL_TOOLTIP },
	["QuestSpamRaid"] = {toggle = "raid", text = QUESTSPAM_RAID_LABEL, tooltip = QUESTSPAM_RAID_TOOLTIP },
	["QuestSpamParty"] = {toggle = "party", text = QUESTSPAM_PARTY_LABEL, tooltip = QUESTSPAM_PARTY_TOOLTIP },
	["QuestSpamGuild"] = {toggle = "guild", text = QUESTSPAM_GUILD_LABEL, tooltip = QUESTSPAM_GUILD_TOOLTIP },
	["QuestSpamChannel"] = {toggle = "channel", text = QUESTSPAM_CHANNEL_LABEL, tooltip = QUESTSPAM_CHANNEL_TOOLTIP },
	["QuestSpamChannelId"] = {field = "channelid", text = QUESTSPAM_CHANNELID_LABEL, tooltip = QUESTSPAM_CHANNELID_TOOLTIP },
	["QuestSpamAchievements"] = {toggle = "achievement", text = QUESTSPAM_ACHIEVEMENTS_LABEL, tooltip = QUESTSPAM_ACHIEVEMENTS_TOOLTIP },
	["QuestSpamDetails"] = {toggle = "details", text = QUESTSPAM_QUESTDETAILS_LABEL, tooltip = QUESTSPAM_QUESTDETAILS_TOOLTIP },
	["QuestSpamDiscovery"] = {toggle = "discovery", text = QUESTSPAM_DISCOVERY_LABEL, tooltip = QUESTSPAM_DISCOVERY_TOOLTIP },
	["QuestSpamCombatXP"] = {toggle = "killxp", text = QUESTSPAM_COMBATXP_LABEL, tooltip = QUESTSPAM_COMBATXP_TOOLTIP },
	["QuestSpamLevelUp"] = {toggle = "levelup", text = QUESTSPAM_LEVELUP_LABEL, tooltip = QUESTSPAM_LEVELUP_TOOLTIP },
	["QuestSpamQuestProgress"] = {toggle = "questxp", text = QUESTSPAM_QUESTPROGRESS_LABEL, tooltip = QUESTSPAM_QUESTPROGRESS_TOOLTIP },
}

-- ========= Functions =======================================================

-- ===========================================================================
-- NAME : QuestSpamOptions_CancelOrLoad(panel)
-- DESC : Called when the Cancel is pressed
-- PARAMS
-- panel : frame : options panel frame
-- ===========================================================================
function QuestSpamOptions_Cancel(panel)
	QSTrace("QuestSpamOptions:Cancel");
	QS_UpdatedOptions = {};
end

-- ===========================================================================
-- NAME : QuestSpamOptions_Close(panel)
-- DESC : Called when the ok button is pressed; This will commit the changes
-- PARAMS
-- panel : frame : options panel frame
-- ===========================================================================
function QuestSpamOptions_Close(panel)
	if (panel) then
		local control;
		local parentId = panel:GetName();
		-- This will save the values
		QSTrace("QuestSpamOptions:Close");
		for key, val in pairs(QuestSpamOptionLabels) do
			control = _G[(parentId .. key)];
			if (control) then
				if (val.toggle and control.type == CONTROLTYPE_CHECKBOX) then
					QuestSpamPref[val.toggle] = tostring(control:GetChecked()) == "1";
				end
				if (val.field and QuestSpamPref[val.field] ~= nil and control.type == CONTROLTYPE_EDITBOX) then
					QuestSpamPref[val.field] =  control:GetText();
				end
			end
		end
		QuestSpamLoadOptionValues();
	end
end

-- ===========================================================================
-- NAME : QuestSpamOptions_OnLoad(panel)
-- DESC : handles the options panel frame onload event
-- PARAMS
-- panel : frame : options panel frame
-- ===========================================================================
function QuestSpamOptions_OnLoad(panel)
	QSTrace("QuestSpamOptions_OnLoad");
    QuestSpamOptionsFrame = panel;
	panel.name = "Quest Spam" -- .. GetAddOnMetadata("QuestSpam", "Version");
	panel.okay = function (panel) QuestSpamOptions_Close(panel); end;
	panel.cancel = function (panel)  QuestSpamOptions_Cancel(panel);  end;
	InterfaceOptions_AddCategory(panel);
	
	if ( QS_VariablesLoaded ) then
		QuestSpamLoadOptionValues();
		QS_OptionsLoaded = true;
	end
end

-- ===========================================================================
-- NAME : QuestSpamLoadOptionValues()
-- DESC : handles the checkboxes on the options panel
-- ===========================================================================
function QuestSpamLoadOptionValues()
	local panel = QuestSpamOptionsFrame;
	if (panel) then
		local label,control;
		local parentId = panel:GetName();
		for key, val in pairs(QuestSpamOptionLabels) do
			-- Set the label text, tooltip, etc (I derived this pattern from Blizzard's OptionsPanelTemplates.lua Nice!);
			control = _G[(parentId .. key)];
			label = _G[(parentId .. key .. "Text")];
			if (control) then
				if (val.tooltip) then
					control.tooltipText = val.tooltip;
				end
				if (val.toggle and control.type == CONTROLTYPE_CHECKBOX) then
					control:SetChecked(QuestSpamPref[val.toggle]);
					control.toggle = val.toggle;
				end
				if (val.field and QuestSpamPref[val.field] ~= nil and control.type == CONTROLTYPE_EDITBOX) then
					control:SetText(QuestSpamPref[val.field]);
					control:SetCursorPosition(0);
				end
			end
			if (label and val.text) then
				label:SetText(val.text);
			end
		end
	end
end

-- ===========================================================================
-- NAME : QuestSpamOptionsPanel_CheckButton_OnClick(checkbox)
-- DESC : handles the checkboxe changes on the options panel; It does not commit them though unless you click OK
-- PARAMS
-- checkbox : CheckButtonElement : options panel checkbutton object
-- ===========================================================================
function QuestSpamOptionsPanel_CheckButton_OnClick(checkbox)
	if ( checkbox.type == CONTROLTYPE_CHECKBOX and checkbox.toggle ) then

	end
end